package com.ccb.health.controller;

import com.ccb.health.entity.HealthInfo;
import com.ccb.health.entity.User;
import com.ccb.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    private UserService userService;

    private String prefix = "/admin";

    /**
     * 返回管理员页面
     * @return
     */
    @GetMapping("/admin")
    public String admin(){
        return prefix + "/admin";
    }

    /**
     * 返回查看用户登记信息界面
     * @return
     */
    @GetMapping("/viewUserInfo")
    public String viewUserInfo(){
        return prefix + "/viewUserInfo";
    }

    /**
     * 返回用户登记信息记录
     * @return
     */
    @PostMapping("/viewUserInfo")
    @ResponseBody
    public Map<String, Object> getUserInfo(){
        List<HealthInfo> res = userService.selectAllUserInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("rows", res);
        map.put("code", 0);
        map.put("total", res.size());
        return map;
    }

    /**
     * 返回查看未登记人员信息
     * @return
     */
    @GetMapping("/viewUnhealthInfo")
    public String viewUnhealthInfo(){
        return prefix + "/viewUnhealthInfo";
    }

    /**
     * 返回未登记人员信息
     * @return
     */
    @PostMapping("/viewUnhealthInfo")
    @ResponseBody
    public Map<String, Object> getUnhealthInfo(){
        List<HealthInfo> res = userService.selectUnhealthUser();
        Map<String, Object> map = new HashMap<>();
        map.put("rows", res);
        map.put("code", 0);
        map.put("total", res.size());
        return map;
    }

    /**
     * 返回编辑用户信息界面
     * @return
     */
    @GetMapping("/userManage")
    public String userManage(){
        return prefix + "/userManage";
    }

    /**
     * 返回用户列表
     * @return
     */
    @PostMapping("userManage")
    @ResponseBody
    public Map<String ,Object> getUserManager(){
        List<User> res = userService.selectUser(new User());
        Map<String, Object> map = new HashMap<>();
        map.put("rows", res);
        map.put("code", 0);
        map.put("total", res.size());
        return map;
    }

    /**
     * 编辑用户信息界面
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editUser")
    public String editUser(int id, ModelMap mmap){
        User user = new User();
        user.setId(id);
        user = userService.selectUser(user).get(0);
        mmap.addAttribute("user", user);
        return prefix + "/editUser";
    }

    /**
     * 提交编辑信息
     * @param user
     * @return
     */
    @PostMapping("/editUser")
    public String editUser(User user){
        int i  = userService.updateUser(user);
        return "redirect:/viewUnhealthInfo";
    }

    /**
     * 返回新增用户界面
     * @return
     */
    @GetMapping("/addUser")
    public String addUser(){
        return prefix + "/addUser";
    }

    /**
     * 保存新增用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public String addUser(User user){
        int i = userService.insertUser(user);
        return "redirect:/viewUnhealthInfo";
    }

}
